package com.realgodo.site.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.ActionSupport;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.entity.vocation.UserVocation;
import com.realgodo.site.service.UserManager;
import com.realgodo.site.service.UserVocationManager;

public class SearchController extends ActionSupport {

	private static final long serialVersionUID = -5057299317966801321L;

	@Autowired
	private UserManager userManager;
	@Autowired
	private UserVocationManager userVocationManager;
	private String key;
	/**Vocation.id*/
	private Long vid;

	private Page<User> userPage = new Page<User>(20);

	public String index() throws Exception {
		if(null==getVid()){
			User userForm = new User();
			userForm.setEnable(User.ENABLED_DISENABLED_ALL);
			userForm.setMeiMeiMan(User.MEIMEI_MAN_ALL);
			userForm.setMeiMeiMember(User.MEIMEI_MEMBER_ALL);
			userForm.setRecommend(User.RECOMMEND_ALL);
			userForm.setKey(getKey().trim());
			userPage = this.userManager.findUsers(userPage, userForm);
		}else{
			Page<UserVocation> userVocationPage = new Page<UserVocation>(20);
			userVocationPage = this.userVocationManager.getUserByVocations(userVocationPage, getVid());
			Set<User> userSet = new HashSet<User>();
			for(UserVocation vocation :userVocationPage.getResult()){
				userSet.add(vocation.getUser());
			}
			List<User> list = new ArrayList<User>(); 
			for(User user :userSet){
				list.add(user);
			}
			userPage.setResult(list);
		}

		return "search";
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Page<User> getUserPage() {
		return userPage;
	}

	public void setUserPage(Page<User> userPage) {
		this.userPage = userPage;
	}

	public Long getVid() {
		return vid;
	}

	public void setVid(Long vid) {
		this.vid = vid;
	}

	
}
